
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

#include "TaskLoader.h"

using namespace solar;

// bool TaskLoader::loadRayTracingHeliostatIndex(std::string filepath, SolarScene &solarScene) {
//     std::ifstream task_file(filepath);
//     if (task_file.fail()) {
//         throw std::runtime_error("Cannot open the file '" + filepath + "'.");
//     }

//     std::stringstream task_stream;
//     task_stream << task_file.rdbuf();
//     task_file.close();

//     std::string number_buffer;
//     std::string::size_type character_pos_next_to_num;   // alias of size_t
//     task_stream >> number_buffer;
//     int N;

//     // 1. Check the whether N is valid
//     try {
//         N = std::stoi(number_buffer, &character_pos_next_to_num);       //N为定日镜个数，
//     } catch (exception e) {
//         throw std::runtime_error(
//                 "The number of heliostats '" + number_buffer +
//                 "' is invalid.\nPlease check the 1st element in the file '" + filepath + "'.");
//     }
//     //character_pos_next_to_num代表一个下标，表示stoi转化成int的string的前多少个字符
//     if (character_pos_next_to_num < number_buffer.size() || N <= 0) {
//         throw std::runtime_error(
//                 "The number of heliostats '" + number_buffer +
//                 "' is invalid.\nPlease check the 1st element in the file '" + filepath + "'.");
//     }

//     heliostat_indexes.clear();
//     receiver_indexes.clear();
//     int heliostat_id;
//     bool ans = true;
//     for (int i = 0; i < N; ++i) {

//         //std::cout << "heliostat_index = " << i << endl;

//         // No more heliostat indexes
//         //如果文件中的index数少于文件标明的heliosat个数，报错
//         if (task_stream.rdbuf()->in_avail() == 0) {
//             throw std::runtime_error(
//                     std::to_string(N) + " heliostat(s) are expected. But only got " +
//                     std::to_string(i) + " heliostats.\nPlease check your file '" + filepath +
//                     "'.");
//         }

//         task_stream >> number_buffer;
//         //  non-numerical character in heliostat index
//         try {
//             heliostat_id = std::stoi(number_buffer, &character_pos_next_to_num);
//         } catch (exception e) {         //判断读入的heliosat的index是否合法
//             throw std::runtime_error(
//                     "The " + std::to_string(i + 1) + "-th heliostat index '" + number_buffer +
//                     "' is invalid.\nPlease check your file '" + filepath + "'.");
//         }

//         // 1. Invalid heliostat_id :
//         //  1) non-numerical character;
//         //  2) less than 0;
//         //  3) larger than heliostat array size-1
//         if (character_pos_next_to_num < number_buffer.size() ||
//             heliostat_id < 0 || heliostat_id >= solarScene.getHeliostats().size()) {
//             //???这里为什么从solarScene里拿数据？ throw std::runtime_error(
//                     "The " + std::to_string(i + 1) + "-th heliostat index '" + number_buffer +
//                     "' is invalid.\n Please check your file '" + filepath + "'.");
//         }
//         //判断当前读入的heliosat的index与之前存储的是否重复
//         if (heliostat_indexes.find(heliostat_id) == heliostat_indexes.end()) {
//             heliostat_indexes.insert(heliostat_id);
//             receiver_indexes.insert(solarScene.getHeliostats()[heliostat_id]->getBelongingGridId());
//             //???这里为什么？？？
//         } else {
//             // If the repetitive indexes occurs, print warning messages.
//             std::cout << "Warning: The " + std::to_string(i + 1) + "-th heliostat index '" +
//                          std::to_string(heliostat_id) + "' is repetitive. Please be careful about
//                          that." << std::endl;
//             ans = false;
//         }
//     }

//     // Check whether the number of heliostat indexes is more than N
//     //如果读入N个定日镜后，文件里还有数据，就warning
//     std::string left_part;
//     int n = task_stream.rdbuf()->in_avail();
//     task_stream >> left_part;
//     if (!left_part.empty()) {
//         left_part = task_stream.str();
//         left_part = left_part.substr(left_part.size() - n, n);
//         std::cout << "Warning: '" + left_part + "' is not processed since only " +
//         std::to_string(N) +
//                      " heliostat(s). Please be careful about that." << std::endl;
//         return false;
//     }

//     return ans;
// }

TaskLoader::TaskLoader(size_t heliostat_num)
{
    receiver_indexes.insert(0); // 只有一个接收器
    for (int i = 0; i < heliostat_num; ++i)
    {
        heliostat_indexes.insert(i);
    }
}

auto TaskLoader::getHeliostatIndexesArray() const -> const std::unordered_set<int>&
{
    return heliostat_indexes;
}

auto TaskLoader::getReceiverIndexesArray() const -> const std::unordered_set<int>&
{
    return receiver_indexes;
}
